package com.sunnada.demo.service.serviceimpl;

import com.sunnada.demo.bean.User;
import com.sunnada.demo.dao.UserSpecification;
import com.sunnada.demo.service.UserSpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

/**
 * @program: mybatisproject2
 * @description:测试Specification
 * @author: 李瑞益
 * @create: 2018-11-07 10:27
 **/
@Service
public class UserSpecificationServiceImpl implements UserSpecificationService {

    @Autowired
    private UserSpecification userSpecification;
    @Override
    public List<User> find(String userName) {
        Specification<User> specification = new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate predicate = criteriaBuilder.equal(root.get("username"),userName);
                Predicate predicate2 = criteriaBuilder.like(root.get("username"),"%"+userName +"%");
                criteriaQuery.where(predicate);
                return null;
            }
        };
        List<User> all = userSpecification.findAll(specification);
        return all;
    }
}
